package hfuu.com.cn.teasystemapp;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import hfuu.com.cn.teasystemapp.bean.land;
import hfuu.com.cn.teasystemapp.bean.pick;
import hfuu.com.cn.teasystemapp.util.HttpUtils;
import hfuu.com.cn.teasystemapp.util.SerializableMap;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class pickedit extends AppCompatActivity {
    List<land> landList;
    Map m;
    String id;
    pick pick;
    String res="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pickaddactivity);
        m=new HashMap();
        id="";
        res="";
        Intent o=getIntent();
        Bundle b=o.getExtras();
        pick=new pick();
        SerializableMap serializableMap= (SerializableMap) b.get("map");
        m=serializableMap.getMap();
        landList = new ArrayList<land>();
        landList = (List<land>) o.getSerializableExtra("landlist");
        ImageView back = (ImageView) findViewById(R.id.pickback);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView textView= (TextView) findViewById(R.id.picktitle);
        textView.setText("采摘修改");
        Spinner sptype= (Spinner) findViewById(R.id.type);
        Spinner spland=(Spinner)findViewById(R.id.landname);
        final EditText teanametext= (EditText) findViewById(R.id.teaname);
        final EditText totaltex= (EditText) findViewById(R.id.total);
        teanametext.setText( m.get("teaname").toString());
        totaltex.setText(m.get("picktotal").toString());
        id=m.get("pickid").toString();
        pick.setPickId(id);
        sptype.setDropDownVerticalOffset(30);
        spland.setDropDownVerticalOffset(30);

       // teanametext.setText();
        List data=new ArrayList();

        for (land land:
                landList) {
            data.add(land.getLandName());
        }
        ArrayAdapter ad=new ArrayAdapter(this,R.layout.landitem,R.id.land,data);
        spland.setAdapter(ad);
        spland.setVisibility(View.VISIBLE);

        SpinnerAdapter sa = spland.getAdapter();
        for (int i = 0; i < sa.getCount(); i++) {
           String l= (String) sa.getItem(i);
            if (m.get("picklandname").toString().equals(l)) {
                spland.setSelection(i);
            }
        }
        SpinnerAdapter sa1 = sptype.getAdapter();
        for (int i = 0; i < sa1.getCount(); i++) {
           String l= (String) sa1.getItem(i);
            if (m.get("typename").toString().equals(l)) {
                sptype.setSelection(i);
            }
        }

        Button okb= (Button) findViewById(R.id.pickok);
        Button cab= (Button) findViewById(R.id.pickcancel);
        okb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String teaname=teanametext.getText().toString().trim();
                String total=totaltex.getText().toString().trim();
                if((teaname.length()==0)&&(total.length()==0)){
                    Toast.makeText(pickedit.this,"请输入内容",Toast.LENGTH_SHORT).show();
                    return;
                }
                Pattern pattern=Pattern.compile("^((0)|([1-9][0-9]*))\\.[0-9]+$");
                Pattern p = Pattern.compile("^[-\\+]?[\\d]*$");
                if(!(pattern.matcher(total).matches()||p.matcher(total).matches())){
                    Toast.makeText(pickedit.this,"重量输入数字",Toast.LENGTH_SHORT).show();
                    return;
                }
                pick.setPickTeaname(teaname);
                pick.setPickQuality(Float.parseFloat(total));
                Gson gson=new Gson();
                String pickjson= gson.toJson(pick);
                HttpUtils.doPost("pick/editprocad", pickjson, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Looper.prepare();
                        Toast.makeText(pickedit.this, "网络连接失败", Toast.LENGTH_LONG).show();
                        Looper.loop();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        res=response.body().string();
                        Message msg = new Message();
                        msg.what = 1;
                        uiHandler.sendMessage(msg);
                    }
                });
                pickedit.this.finish();
            }

        });
        cab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               pickedit.this.finish();
            }
        });
        /**
         * 监听下拉列表
         */
        sptype.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String m = (String) parent.getSelectedItem();
                if(m.equals("折采")){
                    pick.setPickOperate(0);
                }else if(m.equals("提手采")){
                    pick.setPickOperate(1);

                }else if(m.equals("双手采")){
                    pick.setPickOperate(2);

                }else if(m.equals("其他")){
                    pick.setPickOperate(3);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spland.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String m = (String) parent.getSelectedItem();
                for (land land:
                        landList) {
                    if(m.equals(land.getLandName()))
                        pick.setPickLandid(land.getLandId());

                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


    }


    private Handler uiHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    if(res.equals("success")){

                        Toast.makeText(pickedit.this, "修改成功", Toast.LENGTH_LONG).show();

                        return;
                    }else  if(res.equals("error")){

                        Toast.makeText(pickedit.this, "修改失败，库存不足", Toast.LENGTH_LONG).show();

                    }
                    break;
            }

        }
    };


}
